package com.nsqt.biz.controller;

import com.nsqt.biz.domain.AgentDO;
import com.nsqt.biz.service.AgentService;
import com.nsqt.common.utils.*;
import com.nsqt.system.domain.UserDO;
import com.nsqt.system.service.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 代理商
 */
@Controller
@RequestMapping("/biz/agent")
public class AgentController {
    @Autowired
    private AgentService agentService;
    @Autowired
    private UserService userService;

    @GetMapping()
    @RequiresPermissions("biz:agent:agent")
    String Agent() {
        return "biz/agent/agent";
    }

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("biz:agent:agent")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Long currUserId = ShiroUtils.getUserId();
        UserDO currUser = userService.get(currUserId);
        List<Long> roles = currUser.getRoleIds();

        Query query = new Query(params);
        String agent_name = (String) query.get("name");
        if ((roles.contains(new Long(3)))) {
            query.put("businessMan", currUserId);
        } else if (query.get("businessMan") != null && query.get("businessMan") != "") {
            query.put("name", query.get("businessMan"));
            query.remove("userName");
            UserDO businessMan = userService.list(query).get(0);
            query.remove("name");
            query.put("name", agent_name);
            query.put("businessMan", businessMan.getUserId());
        } else {

        }
        List<AgentDO> agentList = agentService.list(query);
        for (AgentDO agent : agentList
        ) {
            Integer userId = agent.getBusinessMan();
            UserDO user = userService.get(new Long(userId));
            agent.setPreserved(user.getName());
        }
        int total = agentService.count(query);
        PageUtils pageUtils = new PageUtils(agentList, total);
        return pageUtils;
    }

    /**
     * 新增代理商
     */
    @GetMapping("/add")
    @RequiresPermissions("biz:agent:add")
    String add(Model model) {
        List<UserDO> userList = userService.list(new HashMap<>());
        model.addAttribute("userList", userList);
        Long userId = ShiroUtils.getUserId();
        UserDO user = userService.get(userId);
        model.addAttribute("businessMan", user);

        return "biz/agent/add";
    }

    /**
     * 编辑代理商
     */
    @GetMapping("/edit/{id}")
    @RequiresPermissions("biz:agent:edit")
    String edit(@PathVariable("id") Long id, Model model) {
        AgentDO agent = agentService.get(id);
        model.addAttribute("agent", agent);
        List<UserDO> userList = userService.list(new HashMap<>());
        model.addAttribute("userList", userList);
        Long userId = ShiroUtils.getUserId();
        UserDO user = userService.get(userId);
        model.addAttribute("businessMan", user);

        return "biz/agent/edit";
    }

    /**
     * 保存代理商
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("biz:agent:add")
    public R save(AgentDO agent) {
        if (agentService.save(agent) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改代理商
     */
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("biz:agent:edit")
    public R update(AgentDO agent) {

        agentService.update(agent);
        return R.ok();
    }

    /**
     * 删除代理商
     */
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("biz:agent:remove")
    public R remove(Long id) {
        if (agentService.remove(id) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 批量删除代理商
     */
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("biz:agent:batchRemove")
    public R remove(@RequestParam("ids[]") Long[] ids) {
        agentService.batchRemove(ids);
        return R.ok();
    }


    /**
     * 删除代理商
     */
    @PostMapping("/getAddress")
    @ResponseBody
    public R getAddress(HttpServletRequest request, Long agentId) {
        Long currUserId = ShiroUtils.getUserId();
        UserDO currUser = userService.get(currUserId);
        String basePath = request.getHeader("REFERER");
        String liveAddress = MD5Utils.encrypt(currUser.getUserId().toString()) + MD5Utils.encrypt(agentId.toString());
        //Todo:设计一个有效地址表，加上地址失效日期
        currUser.setLiveAddress(liveAddress);
        if (userService.update(currUser) > 0) {
            return (R.ok(basePath.substring(0, basePath.length() - 6) + "/infocoll/simcoll?seckey=" + liveAddress));

        }
        return R.error();
    }
}
